#! /bin/bash

source ./scheme.sh
source version.sh

if  [ $# -ne 1 ]; then
    echo -e "${BRed} USAGE: ./trans_pack.sh dev|pre|online|ip ${RCol}"
    exit 1
fi

# ssh/scp port
sshport=12014

destination="${1}"
user="root"
built_folder="src"
case ${destination} in
    "dev")
        ip="192.168.1.151"
        dbconfig="db_dev.php" 
        sshport=12014
        upload="/home/www/agent_adserver/agent_adserver_uploads"
		built_folder=${tagdate}
		source ./web-build.sh
        ;;
    "pre")
        ip="60.191.140.217"
        dbconfig="db_pre.php" 
        sshport=12014
        upload="/home/www/agent_adserver/agent_adserver_uploads"
		built_folder=${tagdate}
        source ./web-build.sh
        ;;
    "online")
        ip="61.174.52.50"
        dbconfig="db_online.php" 
        sshport=12014
        upload="/home/www/agent_adserver/agent_adserver_uploads"
		built_folder=${tagdate}
        source ./web-build.sh 
        ;;
    *)
        ip=${destination}
        upload="/home/www/agent_adserver/agent_adserver_uploads"
        dbconfig="db_local.php" 
		built_folder="src"
        ;;
esac

envDir="config/params/envDir.php"

`rm -f ${envDir}`
`echo "<?php return 'built/${built_folder}';" >> ${envDir}`
`mkdir -p web/built/${built_folder}`

# server config
# use rsa private key instead of plain password
# test_server_ip=$1
dirname="/home/www/agent_adserver/"
dir_ln_name=${dirname}"ui"
dir_ui=${dirname}"agtads_springs/"${tagdate}
dir_keep=${dir_ui}"/web/admath"

# RSA private key file
rsa="./pkey"

# for variables in tag_and_pack.sh
source ./tag_and_pack.sh

if [[ $? -eq 0 ]]
then
    echo -e "Cleanning destination ui directory......${RCol}"
else
    exit -e "${BRed}Tag and Pack failed!${RCol}"
fi

# rm all old files but not dir in ${dir_keep} 
# mv -f ${dir_keep} ../
ssh -p ${sshport} -t -i ${rsa} ${user}@${ip} " mkdir -p ${dir_ui}
"
# cd ${dir_ui}
# ls -1tr | grep -v ${dir_keep} | xargs -d '\n' rm -Rf

echo -e "${Gre}Starting transfer ${TAR} to ${ip}......${RCol}"
# cp tar file to test server 
scp -P ${sshport} -i ${rsa} `pwd`/${TAR} ${user}@${ip}:${dir_ui}

# unpack file and setup
ssh -p ${sshport} -t -i ${rsa} ${user}@${ip} " cd ${dir_ui}
sudo tar -zxf ${TAR}
sudo chmod 777 install.sh
sudo chmod 777 requirements.php
sudo php requirements.php
sudo ./install.sh ${destination}
sudo rm -f ${TAR}
sudo rm -f install.sh tag_and_pack.sh trans_pack.sh scheme.sh version.sh tz.php web-build.sh
sudo rm -rf web/response
sudo rm -f README.md
sudo rm -f composer.json
sudo rm -f composer.lock
sudo rm -f composer.phar
sudo rm -f LICENSE.md
sudo rm -f yii.bat
sudo rm -f web-build.sh
sudo rm -rf webpack.config.js
sudo rm -rf node_modules
sudo rm -f package.json
sudo rm -f web-built.sh
sudo rm -f Gruntfile.js
sudo rm -rf runtime/*
sudo rm -f web/index_dev.php web/index_pre.php web/index_online.php web/index-test.php
sudo rm -f requirements.php
sudo rm -f config/db/db_dev.php config/db/db_pre.php config/db/db_online.php 
sudo rm -f config/package/package_pre.php config/package/package_online.php config/package/package_dev.php 
sudo rm -f config/ssp/ssp_dev.php config/ssp/ssp_pre.php config/ssp/ssp_online.php 
sudo rm -f config/accuen/accuen_dev.php config/accuen/accuen_pre.php config/accuen/accuen_online.php 
sudo rm -f config/redis/redis_dev.php config/redis/redis_pre.php config/redis/reids_online.php 
sudo rm -f config/params/params_dev.php config/params/params_pre.php config/params/params_online.php  
sudo rm -fr data
sudo rm -fr tests
sudo rm -rf web/response
sudo rm -rf web/admath
sudo rm -rf web/built/build.txt
sudo rm -rf web/built/css
sudo rm -rf web/built/fonts
sudo rm -rf web/built/img
sudo rm -rf web/built/js
sudo rm -rf web/built/sass
sudo rm -rf envfile
sudo ln -s ${upload} web/admath
sudo rm -rf ${dir_ln_name}
sudo ln -s ${dir_ui} ${dir_ln_name}
"

rm -f web/ver.html
rm -f web/install.log


# remove host tar
rm ${TAR}
rm -rf views/site/ver.php
rm -f ${envDir}
echo "<?php return 'src';" >> ${envDir}
rm -rf web/built/${built_folder}

